# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Contributor: Milan P. Stanić <mps@arvanta.net>
# Maintainer: Jakub Jirutka <jakub@jirutka.cz>
pkgname=crystal
pkgver=0.35.1
pkgrel=0
_bootver=0.35.0
_llvmver=10
pkgdesc="The Crystal Programming Language"
url="https://crystal-lang.org/"
arch="x86_64 aarch64" # reenable aarch64
license="Apache-2.0"
depends="gc-dev libatomic_ops libevent-dev libevent-static gcc gmp-dev pcre-dev"
checkdepends="openssl-dev libxml2-dev readline-dev tzdata yaml-dev zlib-dev"
makedepends="llvm$_llvmver-dev llvm$_llvmver-static zlib-static"
subpackages="$pkgname-doc
	$pkgname-bash-completion:bashcomp:noarch
	$pkgname-zsh-completion:zshcomp:noarch
	"
source="$pkgname-$pkgver.tar.gz::https://github.com/crystal-lang/$pkgname/archive/$pkgver.tar.gz
	https://dev.alpinelinux.org/archive/crystal/crystal-$_bootver-x86_64-alpine-linux-musl.tar.gz
	https://dev.alpinelinux.org/archive/crystal/crystal-$_bootver-aarch64-alpine-linux-musl.tar.gz
	fix-version-string.patch
	global_isel.patch
	c_abi.patch
	valist.patch
	"
builddir="$srcdir/$pkgname-$pkgver"

_coredir="/usr/lib/$pkgname/core"
_shardsdir="/usr/lib/$pkgname/shards"

export CRYSTAL_CONFIG_VERSION="$pkgver"
export CRYSTAL_CACHE_DIR="$srcdir/.cache"

# Build static crystal binary and upload it to dev.a.o, so it can be used
# for building crystal next time (needed for bootstrapping).
# NOTE: After https://github.com/crystal-lang/crystal/issues/5689 is fixed,
# we can also use prebuilt binary from upstream if needed.
snapshot() {
	local binary="$builddir/.build/crystal"
	local tarname="$pkgname-$pkgver-$CTARGET"

	if [ ! -f "$binary" ] || ! file "$binary" | grep -qw 'statically linked'; then
		msg "Building statically linked crystal..."
		BUILD_STATIC=1 abuild clean deps unpack prepare build
	fi

	cd "$srcdir"

	strip "$builddir"/.build/crystal
	install -D -m 755 "$builddir"/.build/crystal "$tarname"/bin/crystal
	tar -czf "$tarname.tar.gz" "$tarname"

	msg "Uploading $tarname.tar.gz to dev.alpinelinux.org"
	scp "$tarname.tar.gz" dev.alpinelinux.org:/archive/$pkgname/

	APKBUILD="../APKBUILD" abuild undeps
}

prepare() {
	default_prepare

	cat > Makefile.local <<-EOF
		progress = 1
		threads = ${JOBS:-2}
		verbose = 1
		FLAGS = --verbose --target $CTARGET ${BUILD_STATIC:+"--link-flags=-no-pie"}
		LLVM_CONFIG = /usr/lib/llvm$_llvmver/bin/llvm-config
	EOF
}

build() {
	make crystal \
		CRYSTAL_CONFIG_PATH="lib:$_shardsdir:$_coredir" \
		PATH="$srcdir/$pkgname-$_bootver-$CBUILD/bin:$PATH" \
		LLVM_CONFIG="llvm-config" \
		release=1 \
		static=${BUILD_STATIC:-}
}

check() {
	make spec LLVM_CONFIG="llvm-config" PATH=".build:$PATH" \
	SPEC_FLAGS="--no-color --verbose"
}

package() {
	install -D -m 755 .build/crystal "$pkgdir"/usr/bin/crystal
	install -D -m 644 man/crystal.1 "$pkgdir"/usr/share/man/man1/crystal.1

	mkdir -p "$pkgdir$_coredir" "$pkgdir$_shardsdir"
	cp -r src/* "$pkgdir$_coredir"/

	cd "$pkgdir$_coredir"
	rm ext/sigfault.* llvm/ext/llvm_ext.o
}

bashcomp() {
	pkgdesc="Bash completions for $pkgname"
	depends=""
	install_if="$pkgname=$pkgver-r$pkgrel bash-completion"

	cd "$builddir"
	install -D -m 644 etc/completion.bash \
		"$subpkgdir"/usr/share/bash-completion/completions/$pkgname
}

zshcomp() {
	pkgdesc="ZSH completions for $pkgname"
	depends=""
	install_if="$pkgname=$pkgver-r$pkgrel zsh"

	cd "$builddir"
	install -D -m 644 etc/completion.zsh \
		"$subpkgdir"/usr/share/zsh/site-functions/_$pkgname
}

sha512sums="0381568330802de5838f8e66600567817b60ba0087bb3cc2b5df2944c5c42779039b606c81e0207bef34082bf25331b590a8140830f65ba4106ae465f717000b  crystal-0.35.1.tar.gz
885c6e76590515bac07d76313c1e453414a08be100f61c9cde02cbc93218ed3048cec2ec060bd413a861a1e5eb51e38213ddc90f41f87bdd517fd22a0af1554f  crystal-0.35.0-x86_64-alpine-linux-musl.tar.gz
8ae89267dbc10f3fb9a292843725c792bfdb7dadbd48ca46564e6e58bfe816a4747bcc62058b148c673c0c145f5e637cac230150065c12e8b57a71e7ea830973  crystal-0.35.0-aarch64-alpine-linux-musl.tar.gz
1bb7d649841a7b0f66fdebbb75647ef8958ce7fb3437f0a6303ad21750af79becdcad87ddcf9353d48d466495a6c5837171b571a46412fd746c741296a67ad93  fix-version-string.patch
6c41e0e9b50711cbd1979a4a7734346630ef8aa73cfdbe6592c3930681a7e7efb7c75b20653e6f11dc744db53fd45c1c55f58b7994f979f11edec39c2a11c360  global_isel.patch
a870ac1129fbca7ec503022e20c714d7f45b9502852d5eed691dbcf4d305003bd952d36e214c28cdc43ac9334e9e7ff5f68321cd0118595519b963e7bd015fd0  c_abi.patch
5bdb0163fcd22e7871800a56a4a0229c391ab47fdd23e3ca642a997b871e683302daefd8cb9ebc042a0791332f491e38660005bd5e889ce57e2e32ac1a7e2053  valist.patch"
